package com.tlr.device.rest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.tfb.rong.Device;
import com.tfb.rong.Users;
import com.tfb.service.UserAssessmentI;
import com.tfb.service.utility.DeviceTask;
import com.tlr.device.model.LocationCard;
import com.tlr.device.model.Result;
import com.tlr.device.service.DeviceDatabaseService;
import com.tlr.device.service.LocationDevice;

@Path("/location_device")
public class LocationDeviceResource {

    private static final Logger log = Logger.getLogger(LocationDeviceResource.class);
    @Context
    ServletContext context;
    DeviceDatabaseService deviceService;
    UserAssessmentI userService;

    private synchronized void initDatabaseService() {
        if (deviceService == null || userService == null) {
            ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(context);
            if (deviceService == null) {
                deviceService = (DeviceDatabaseService) ac.getBean("deviceDatabaseService");
            }
            if (userService == null) {
                userService = (UserAssessmentI) ac.getBean("userAssessmentService");
            }
        }
    }

    @Path("/bind_device/{user}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Result<List<Device>> getUserBindLocationDeviceList(@PathParam("user") String user) throws IOException {
        try {
            initDatabaseService();
            List<Device> devices = deviceService.getAllBindDevice(user, DeviceTask.DEVICE_LOCATION);
            return new Result<>(0, "成功", devices);
        } catch (Exception e) {
            log.error(e.fillInStackTrace());
            return new Result<>(500, "读取用户定位卡失败");
        }
    }

    @Path("/device_data/{deviceid}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Result<List<LocationCard>> getLocationDeviceData(@PathParam("deviceid") String deviceid) {
        try {
            initDatabaseService();
            List<LocationCard> devices = deviceService.getCurrentLocationData(deviceid);
            return new Result<>(0, "成功", devices);
        } catch (Exception e) {
            log.error(e.fillInStackTrace());
            return new Result<>(500, "读取定位卡失败");
        }
    }

    @Path("/locations")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Result<List<LocationCard>> listLocation() throws IOException {
        try {
            initDatabaseService();
            List<Device> allDevice = deviceService.getAllDevice(2, 1);
            List<LocationCard> locations = new ArrayList<>();
            LocationDevice ld = new LocationDevice();
            for (Device device : allDevice) {
                String msid = device.getDevicemac();
                LocationCard lc = ld.getLocationDevice(AppContext.getLocationCardGroupID(), msid);
                lc.setStorage_id(device.getDeviceid());
                lc.setDevice_id(device.getDevicemac());
                Users user = new Users();
                if (device.getBinduser() != null && !device.getBinduser().isEmpty()) {
                    user.setId(device.getBinduser());
                    user = userService.selectBindUserInfo(user);
                    if (user != null) {
                        lc.setPlate(device.getBindname());
                        lc.setMobile(user.getTel());
                        lc.setOwner(user.getName());
                    }
                }
                if (lc.getOwner() == null || lc.getOwner().isEmpty()) {
                    lc.setPlate("未绑定");
                    lc.setMobile("未绑定");
                    lc.setOwner("未绑定");
                }
                if (!lc.isEnable()) {
                    lc.setStatus_label("停止服务");
                } else {
                    lc.setStatus_label("服务中");
                }
                locations.add(lc);
            }
            return new Result<>(0, "成功", locations);
        } catch (Exception e) {
            log.error(e.fillInStackTrace());
            return new Result<>(500, "读取定位卡失败");
        }
    }
}
